#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------


Global $winwork[$max_client + 1]
Global $winprogress[$max_client + 1]

Func make_progress($pos)
   $winprogress[$pos] += 1
EndFunc


Func Search()
   Local $win = WinList($title_ff)
   Local $l= 0,$t = 0,$r = 300,$b = 400
   Local $total_win = $win[0][0]
   If $total_win > $max_client Then Return $total_win
   
   Local $hnd[$total_win][3]
   For $i = 1 To $total_win
	  Local $hWnd = $win[$i][1]
	  Local $pos = WinGetPos($hWnd)
	  $hnd[$i-1][0] = $pos[0]
	  $hnd[$i-1][1] = $pos[1]
	  $hnd[$i-1][2] = $hWnd
   Next
   
   For $i = 0 To $total_win-2
	  For $j = $i+1 To $total_win-1
		 If $hnd[$i][0] > $hnd[$j][0] Or ($hnd[$i][0] = $hnd[$j][0] And $hnd[$i][1] > $hnd[$j][1]) Then
			For $k = 0 To 2
			   Local $tmp = $hnd[$i][$k]
			   $hnd[$i][$k] = $hnd[$j][$k]
			   $hnd[$j][$k] = $tmp
			Next
		 EndIf
	  Next
   Next
   
   For $i = 0 To $total_win-1
	  Local $hWnd = $hnd[$i][2]
	  WinActivate($hWnd)
	  Local $w = $r - $l + 1
	  Local $h = $b - $t + 1
	  Local $length = $w * $h

	  Local $st = TimerInit()
	  Local $hBMP = _ScreenCapture_CaptureWnd("", $hWnd, $l, $t, $r, $b, False)
	  ;Local $filename = "win-" & $wpos[0] & "x" & $wpos[1] & ".jpg"
	  ;_ScreenCapture_SaveImage($filename,$hBMP,False)

	  Local $tBits = DllStructCreate('dword[' & $length & ']')
	  _WinAPI_GetBitmapBits($hBMP, 4 * $length, DllStructGetPtr($tBits))
	  Local $rest = TimerDiff($st)

	  ;_DebugOut("c1=" & Hex(getPixel($tBits,$w,$test[0][0],$test[0][1]),6) & " c2="  & Hex(getPixel($tBits,$w,$test[1][0],$test[1][1]),6))

	  Local $f = False
	  For $shift = 1 To $length
		 If $anchor[2] = BitAND(DllStructGetData($tBits, 1, $shift),0xffffff) _
		 And $anchor[3] = BitAND(DllStructGetData($tBits, 1, $shift+1),0xffffff) Then
			Local $x = Mod($shift,$w) - 1
			Local $y = Floor($shift/$w)
			$f = True
			$winlist[0] += 1
			Local $fw[$index_action + UBound($actions)*2] = [$hnd[$i][0], $hnd[$i][1], $x - $anchor[0], $y - $anchor[1], $hWnd];wx,wy,fx,fy,hnd,
			$winlist[$winlist[0]] = $fw
			_DebugOut($rest & " point= " & $x & "x" & $y & " shift= " & $fw[2] & "x" & $fw[3] & " wpos= " & $hnd[$i][0] & "x" & $hnd[$i][1])
			ExitLoop
		 EndIf
	  Next
	  _WinAPI_DeleteObject($hBMP)
	  _WinAPI_DeleteObject($tBits)
   Next
   Return $winlist[0]
EndFunc

Func getPixel(ByRef $tBits,$width,$x,$y)
   Local $shift = $x + $y * $width + 1
   If $width < 1 Or $x < 0 Or $y < 0 Then
		_DebugOut("Bad param getPixel(" & $w & "," & $x & "," & $y & ")")
	EndIf
   return BitAND(DllStructGetData($tBits, 1, $shift),0xffffff)
EndFunc

Func getPixelLight(ByRef $tBits,$width,$x,$y)
	Local $shift = $x + $y * $width + 1
	Return BitAND(BitShift(DllStructGetData($tBits, 1, $shift), 16), 0xFF)
EndFunc


Func Show($pos)
   Local $p = $winlist[$pos]
   WinActivate($p[$index_handle])
EndFunc

Func Work($pos)
   If $flag_work[$pos] Then
	  StopWork($pos)
   Else
	  StartWork($pos)
   EndIf
   check_work()
EndFunc

Func check_work()
   Local $has_work = False
   For $i = 1 To $flag_work[0]
	  If $flag_work[$i] Then
		 $has_work = True
		 ExitLoop
	  EndIf
   Next
   If $timer = -1 And $has_work Then
	  $timer = _Timer_SetTimer($hGUI,100,"process_work")
	  _DebugOut("timer started")
	  HotKeySet("{ESC}","StopAll")
   ElseIf Not ($timer = -1 Or $has_work) Then
	  _Timer_KillTimer($hGUI,$timer)
	  $timer = -1
	  _DebugOut("timer done")
	  HotKeySet("{ESC}")
   EndIf
   Return $has_work
EndFunc

Func StartWork($pos)
   Local $p = $winlist[$pos]
   If $flag_work[$pos] Then Return False

   Local $has = False
   For $i = 0 To UBound($actions) - 1
	  ;_DebugOut("i=" & $i & " h=" & $p[$i + $index_action] & " v=" & GUICtrlRead($p[$i + $index_action]))
	  If BitAND($GUI_CHECKED,GUICtrlRead($p[$i + $index_action])) Then 
		 $has = True
		 $need_action[$pos][$i] = True
	  Else
		 $need_action[$pos][$i] = False
	  EndIf
   Next
   If Not $has Then Return False

   Local $ta = UBound($actions)
   For $i = 0 To $ta - 1
	  GUICtrlSetState($p[$i + $index_action],$GUI_DISABLE)
	  GUICtrlSetState($p[$i + $index_action + $ta],$GUI_DISABLE)
   Next
   GUICtrlSetData($p[$index_btn],"stop")
   $flag_work[$pos] = True
   $step[$pos] = 0
   $substep[$pos] = 0
   $stepdata[$pos] = False
   $nextenter[$pos] = 0
   $winwork[$pos] = TimerInit()
   $winprogress[$pos] = 0
EndFunc

Func StopWork($pos,$str = "stop")
   Local $p = $winlist[$pos]
   GUICtrlSetData($p[$index_btn],"make")
   $flag_work[$pos] = False
   GUICtrlSetData( _
		 $p[$index_label],$str _
		 & " tm: " & Round(TimerDiff($winwork[$pos]) / 1000 / 60, 1) _
		 & " p: " & $winprogress[$pos] _
	  )
   Local $ta = UBound($actions)
   For $i = 0 To $ta - 1
	  GUICtrlSetState($p[$i + $index_action],$GUI_ENABLE)
	  GUICtrlSetState($p[$i + $index_action + $ta],$GUI_ENABLE)
   Next
EndFunc

Func StopAll()
   For $pos = 1 To $flag_work[0]
	  StopWork($pos)
   Next
EndFunc

